﻿using AutoMapper;
using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Read.API.Command.RoleCommand;
using B.S.BaseData.Read.API.DTO;
using MediatR;

namespace B.S.BaseData.Read.API.CommandHandler.RoleHandler
{
    public class QueryRoleHandler : IRequestHandler<QueryRoleCommand, APIResult<List<RoleDTO>>>
    {
        private readonly IBaseRepository<Role> roleRep;
        private readonly IMapper mapper;

        public QueryRoleHandler(IBaseRepository<Role> roleRep,IMapper mapper)
        {
            this.roleRep = roleRep;
            this.mapper = mapper;
        }
        public Task<APIResult<List<RoleDTO>>> Handle(QueryRoleCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<RoleDTO>> result = new APIResult<List<RoleDTO>>();
            result.Code=APIEnum.Success;
            result.Msg = "查询成功";

            var role = roleRep.GetAll().Where(x=>!x.IsDeleted).OrderByDescending(p=>p.RoleId);
            result.Data=mapper.Map<List<RoleDTO>>(role);
            return Task.FromResult(result);
        }
    }
}
